package com.gdut.simple.model.impl;

import com.gdut.simple.basic.BasicApplication;
import com.gdut.simple.common.LogUtils;
import com.gdut.simple.common.RequestCallback;
import com.gdut.simple.common.ToastUtils;
import com.gdut.simple.data.entity.Comment;
import com.gdut.simple.data.entity.Post;
import com.gdut.simple.data.entity.User;
import com.gdut.simple.model.IPostDetailModel;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

public class PostDetailModelImpl implements IPostDetailModel {

    @Override
    public void getCommentList(Post post, final RequestCallback<List<Comment>> callback) {
        BmobQuery<Comment> query = new BmobQuery<>();
        query.addWhereEqualTo("post", post);
        query.include("user,post");
        query.findObjects(new FindListener<Comment>() {
            @Override
            public void done(List<Comment> list, BmobException e) {
                if (e == null) {
                    if (callback != null) {
                        callback.onSuccess(list);
                    }
                } else {
                    if (callback != null) {
                        callback.onFailure(e.getMessage());
                    }
                }
            }
        });
    }

    @Override
    public void comment(String content, User user, Post post, final RequestCallback<Comment> callback) {
        final Comment comment = new Comment();
        comment.setContent(content);
        comment.setUser(user);
        comment.setPost(post);
        comment.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                if (e == null) {
                    if (callback != null) {
                        callback.onSuccess(comment);
                    }
                } else {
                    if (callback != null) {
                        callback.onFailure(e.getMessage());
                    }
                }
            }
        });
    }
}
